package cn.iocoder.boot.controller.admin;

import cn.iocoder.boot.dto.ConfigQueryTO;
import cn.iocoder.boot.dto.SearchModelTO;
import cn.iocoder.boot.entity.SalePriceConfig;
import cn.iocoder.boot.service.SalePriceConfigService;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@RestController
@RequestMapping("/salePriceConfig")
public class SalePriceConfigController {

@Resource
private SalePriceConfigService salePriceService;


    @PostMapping("/list")
    @Operation(summary = "分页")
    public CommonResult<Page<SalePriceConfig>> listSalePriceConfig(@RequestBody ConfigQueryTO to) {
        // 创建 Lambda 查询条件的包装器

        return CommonResult.success(salePriceService.list(to));
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @PermitAll
    public CommonResult<Boolean> updateSalePriceConfig(@RequestBody SalePriceConfig salePriceConfig) {
        return CommonResult.success(salePriceService.updateSalePriceConfig(salePriceConfig));
    }


    @Operation(summary = "新增")
    @PostMapping("/save")
    @PermitAll
    public CommonResult<Boolean> saveSalePriceConfig(@RequestBody SalePriceConfig salePriceConfig) {
        return CommonResult.success(salePriceService.insertSalePriceConfig(salePriceConfig));
    }
    @PostMapping("/delete/{id}")
    @Operation(summary = "删除")
    public CommonResult<Boolean> deleteSalePriceConfig(@PathVariable Long id) {
        return CommonResult.success(salePriceService.deleteSalePriceConfig(id));
    }


    @PostMapping("/searchModel")
    @Operation(summary = "根据型号查询")
    @PermitAll
    public CommonResult<SalePriceConfig> searchModel(@RequestBody SearchModelTO salePriceConfig) {
        return CommonResult.success(salePriceService.searchModel(salePriceConfig));
    }


    @GetMapping("/get/{id}")
    @Operation(summary = "详情")
    public CommonResult<SalePriceConfig> getSalePriceConfig(@PathVariable Long id) {
        return CommonResult.success(salePriceService.getSalePriceConfig(id));
    }


    @PermitAll
    @GetMapping("/get-template")
    @Operation(summary = "获得导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 获取文件所在的路径
        ClassPathResource resource = new ClassPathResource("物料销价配置.xlsx");

        if (resource.exists()) {
            // 设置响应的内容类型为 Excel 文件
            response.setContentType("application/vnd.ms-excel");
            // 设置响应头，告诉浏览器这是一个附件并指定文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + "导入模板");

            // 获取文件输入流并进行输出
            try (InputStream in = resource.getInputStream();
                 OutputStream out = response.getOutputStream()) {

                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.flush();
            }
        } else {
            // 文件不存在时，可以做适当的错误处理
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "模板文件未找到");
        }
    }


    @Operation(summary = "导入库存")
    @PostMapping("/import")
    @PermitAll
    public CommonResult<Boolean> importSalePriceConfig(@RequestParam("file") MultipartFile file) throws IOException {
        return CommonResult.success(salePriceService.importSalePriceConfig(file));
    }



}
